<?php
//PingBuWang.Com
//Copyright (c) 2017~2099 http://www.pingbuwang.com All rights reserved.
namespace app\common\model;
use mip\Paginationm;
use mip\Init;
class Tags extends Init
{

    public function _initialize()
    {
        parent::_initialize();

    }
    public function getItemList($where=null,$order= 'create_time desc',$page = 1, $limit = 100)
    {
        if(!$page || $page==''){
            $page = null;
        }
        if(!$limit || $limit==''){
            $limit = 100;
        }

        $itemList = db('tags')->where($where)->page($page,$limit)->order($order)->select();

        if ($itemList) {
            foreach ($itemList as $k => $v) {
                $itemList[$k]['url'] = $this->domain . '/tag/' . $v['id'] . '/';
            }
        } else {
            $itemList = array();
        }
        return $itemList;
    }
    
    public function getPaginationm($cid = 0, $per_page = 10, $category = null, $sub = null,$where = null)
    {
        $count = $this->getCount($cid);;
        if ($category) {
            $baseUrl = $this->domain . '/tag/' . $category . '/';
            if ($sub) {
                $baseUrl = $this->domain . '/' . $category . '/' . $sub . '/';
            }
        } else {
            $baseUrl = $this->domain . '/tag/';
        }
        $pagination_array = array(
            'base_url' => $baseUrl,
            'total_rows' => $count,
            'per_page' => $per_page,
            'page_break' => config('urlPageBreak')
        );
        $pagination = new Paginationm($pagination_array);
        return $pagination->create_links();
    }
    
    public function getCount($where=null)
    {
        $count = db('tags')->where($where)->count();
        return $count;
    }

    public function getUrl($itemInfo)
    {
        if (!$itemInfo) {
            return false;
        }
        $url = $this->domain . '/tag/' . $itemInfo['id'] . '/';

        return $url;
    }
    
    public function getPageUrl($category,$sub)
    {
        if ($category) {
            $url = $this->domain . '/tag/' . $category . '/';
            if ($sub) {
                $url = $this->domain . '/' . $category . '/' . $sub . '/';
            }
        } else {
            $url = $this->domain . '/tag/';
        }
        if ($this->addonsNameSpace) {
            if ($tempUrl = model($this->addonsNameSpace)->getTagsPageUrl($category,$sub)) {
                $url = $tempUrl;
            }
        }
        return $url;
    }

}